﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Web;
using System.Web.Mvc;
using System.Web.Compilation;
using System.Web.Routing;
using System.Globalization;
using Microsoft.Practices.Unity;
using Vit.Common;
using Vit.Web.Models;
using Vit.Web.Providers;
using Vit.Web.Helpers;

namespace Vit.Web.Mvc
{
    public class ThemedCshtmlViewEngine : BuildManagerViewEngine
    {
        internal static readonly string ViewStartFileName;

        static ThemedCshtmlViewEngine()
        {
            ViewStartFileName = "_ViewStart";
        }

        public ThemedCshtmlViewEngine()
        {
            base.AreaViewLocationFormats = new string[] { 
                "~/Areas/{2}/Views/{1}/{0}.cshtml", 
                "~/Areas/{2}/Views/Shared/{0}.cshtml", 
                "~/Views/{1}/{0}.cshtml",
                "~/Themes/{3}/Pages/{0}.cshtml",
                "~/Themes/Default/Pages/{0}.cshtml",
                "~/Views/Shared/{0}.cshtml",
                "~/Themes/{3}/Pages/Main.cshtml",
                "~/Themes/Default/Pages/Main.cshtml",
                "~/Views/Shared/Main.cshtml"
            };
            base.AreaMasterLocationFormats = new string[] { 
                "~/Themes/{3}/Layouts/{0}.cshtml",
                "~/Themes/Default/Layouts/{0}.cshtml",
                "~/Views/{1}/{0}.cshtml",
                "~/Views/Shared/{0}.cshtml"
            };
            base.AreaPartialViewLocationFormats = new string[]{
                "~/Areas/{2}/Views/{1}/{0}.cshtml", 
                "~/Areas/{2}/Views/Shared/{0}.cshtml", 
                "~/Areas/{2}/Themes/{3}/Editors/{0}.cshtml", 
                "~/Areas/{2}/Themes/{3}/Controls/{0}.cshtml", 
                "~/Areas/{2}/Themes/{3}/Containers/{0}.cshtml", 
                "~/Areas/{2}/Themes/Default/Editors/{0}.cshtml", 
                "~/Areas/{2}/Themes/Default/Controls/{0}.cshtml", 
                "~/Areas/{2}/Themes/Default/Containers/{0}.cshtml", 
                "~/Views/{1}/{0}.cshtml",
                "~/Themes/{3}/Editors/{0}.cshtml",
                "~/Themes/{3}/Controls/{0}.cshtml",
                "~/Themes/{3}/Containers/{0}.cshtml",
                "~/Themes/Default/Editors/{0}.cshtml",
                "~/Themes/Default/Controls/{0}.cshtml",
                "~/Themes/Default/Containers/{0}.cshtml",
                "~/Views/Shared/{0}.cshtml"
            };

            base.ViewLocationFormats = new string[] { 
                "~/Views/{1}/{0}.cshtml",
                "~/Themes/{3}/Pages/{0}.cshtml",
                "~/Themes/Default/Pages/{0}.cshtml",
                "~/Views/Shared/{0}.cshtml",
                "~/Themes/{3}/Pages/Main.cshtml",
                "~/Themes/Default/Pages/Main.cshtml",
                "~/Views/Shared/Main.cshtml"
            };
            base.MasterLocationFormats = new string[] { 
                "~/Themes/{3}/Layouts/{0}.cshtml",
                "~/Themes/Default/Layouts/{0}.cshtml",
                "~/Views/{1}/{0}.cshtml",
                "~/Views/Shared/{0}.cshtml"
            };
            base.PartialViewLocationFormats = new string[]{
                "~/Views/{1}/{0}.cshtml",
                "~/Themes/{3}/Editors/{0}.cshtml",
                "~/Themes/{3}/Controls/{0}.cshtml",
                "~/Themes/{3}/Containers/{0}.cshtml",
                "~/Themes/Default/Editors/{0}.cshtml",
                "~/Themes/Default/Controls/{0}.cshtml",
                "~/Themes/Default/Containers/{0}.cshtml",
                "~/Views/Shared/{0}.cshtml"
            };
            this.ViewStartFileExtensions = new string[] { "cshtml" };
        }

        public override ViewEngineResult FindPartialView(ControllerContext controllerContext, string partialViewName, bool useCache)
        {
            if (partialViewName.Contains("FilterTemplates"))
            {
                partialViewName = partialViewName.Replace("DisplayTemplates/FilterTemplates", "FilterTemplates");
                partialViewName = partialViewName.Replace("EditorTemplates/FilterTemplates", "FilterTemplates");
            }
            return base.FindPartialView(controllerContext, partialViewName, useCache);
        }

        protected override string GetFormatedVirtualPath(HttpContextBase context, VirtualPathProviderViewEngine.ViewLocation location, string name, string controllerName, string areaName)
        {
            string theme = PortalContext.Current.Theme;
            string skin = PortalContext.Current.Skin;
            return string.Format(location.VirtualPathFormatString, name, controllerName, areaName, theme, skin);
        }

        protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath)
        {
            string layoutPath = null;
            bool runViewStartPages = false;
            return new RazorView(controllerContext, partialPath, layoutPath, runViewStartPages, this.ViewStartFileExtensions);
        }

        protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath)
        {
            string layoutPath = masterPath;
            bool runViewStartPages = true;
            return new RazorView(controllerContext, viewPath, layoutPath, runViewStartPages, this.ViewStartFileExtensions);
        }

        protected override bool IsValidCompiledType(ControllerContext controllerContext, string virtualPath, Type compiledType)
        {
            return typeof(WebViewPage).IsAssignableFrom(compiledType);
        }

        public string[] ViewStartFileExtensions { get; set; }
    }
}
